
class Admin::WikiCategoriesController < Admin::BaseController
  before_action :find_wiki_category, only: [:edit, :update, :destroy]

  def index
    @wiki_categories = WikiCategory.all.page(params[:page] || 1).per_page(params[:per_page] || 10)
      .order(id: "desc")
  end

  def new
    @wiki_category = WikiCategory.new
  end

  def create
    @wiki_category = WikiCategory.new(params.require(:wiki_category).permit!)

    if @wiki_category.save
      flash[:notice] = "保存成功"
      redirect_to admin_wiki_categories_path
    else
      render action: :new
    end
  end

  def edit
    render action: :new
  end

  def update
    @wiki_category.attributes = params.require(:wiki_category).permit!

    if @wiki_category.save
      flash[:notice] = "修改成功"
      redirect_to admin_wiki_categories_path
    else
      render action: :new
    end
  end

  def destroy
    if @wiki_category.destroy
      flash[:notice] = "删除成功"
      redirect_to admin_wiki_categories_path
    else
      flash[:notice] = "删除失败"
      redirect_to :back
    end
  end

  private

  def find_wiki_category
    @wiki_category = WikiCategory.find params[:id]
  end

end
